<template>
 

 <el-dialog 
   width="80%" 
  :close-on-click-modal="false"
  :title="title"
  :model-value="isShow"
  @open="onOpen">
    <el-form ref="form" :inline="true" :model="form" :rules="rules" label-width="86px">
      <el-form-item label="标识" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>

      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
     
      <el-form-item label="item标识" prop="itemCode">
        <el-input v-model="form.itemCode" />
      </el-form-item>
      
      <el-form-item label="item描述" prop="itemName">
        <el-input v-model="form.itemName" />
      </el-form-item>
      
      <el-form-item label="排序" prop="seq">
        <el-input v-model="form.seq" />
      </el-form-item>
     
      <el-form-item style="margin-bottom: 0" label="所属应用" prop="clientId">
        <el-select v-model="form.clientId" placeholder="请选择">
          <el-option v-for="item in applications" :key="item.clientId" :label="item.name" :value="item.clientId" />
        </el-select>
      </el-form-item>

      <el-form-item style="margin-bottom: 0" label="所属租户" prop="tenantId">
        <el-select v-model="form.tenantId" placeholder="请选择">
          <el-option v-for="item in tenants" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button  @click="onCancel">取消</el-button>
      <el-button type="primary" @click="onSave">确认</el-button>
    </template>
  </el-dialog>
</template>

<script>

import { getDetail, add, save } from '@/api/admin/dictionary'
import { getAll as getApplication } from "@/api/admin/project";
import { getAll as getTentant } from "@/api/admin/tentant";
export default {
  components: {},
  props: {
    data: {
      // required: true,
      type: String,
      default: ''
    },
    isCopy: {
      type: Boolean,
      default: false,
    },
    isShow: {
      require: true,
      type: Boolean,
      default: false
    },
    title: {
      // require: true,
      type: String,
      default: ""
    }
  },
  emits: ['onChange','update:isShow'],
  data() {
    return {
      dialogVisible: false,
      form: {},
      rules: {},
      applications: [],
      tenants: []
    }
  },
  mounted() {
    getApplication().then((res) => {
      this.applications = res.result
    })

    getTentant().then((res) => {
      this.tenants = res.result
    })
  },
  methods: {
    async onOpen() {
      if (this.data) {
        const user = await getDetail(this.data)
        this.form = user.result
        if(this.isCopy) {
          this.form.id = ''
        }
      }
      else {
        this.form = {
          name: '',
          code: '',
          itemCode: '',
          itemName: '',
          seq: 1,
          clientId: '',
          tenantId: '1'
        }
      }

    },
    async onSave() {
      
      await save(this.form)

      this.$emit("onChange", this.form)
      this.$emit("update:isShow", false)
    },
    onCancel() {
      this.$emit("update:isShow", false)
    },
    onClose() {

    }
  },
  watch: {
    'data': async function () {

    }
  }
}
</script>


